//
//  YKTopic.m
//  baisi
//
//  Created by 吴高烽 on 16/10/5.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKTopic.h"
#import "YKComment.h"
#import "YKUser.h"
@implementation YKTopic{
    CGFloat _cellHeight ;
    CGRect _pictureVF;
}
+(NSDictionary*)mj_replacedKeyFromPropertyName{
    return @{@"small_image":@"image0",
             @"middle_image":@"image1",
             @"large_image":@"image2"};
}
+(NSDictionary*)mj_objectClassInArray{
    return @{@"top_cmt":[YKComment class]
             };
}
-(NSString *)created_at{
    // 日期格式化类
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // 帖子的创建时间
    NSDate *create = [fmt dateFromString:_created_at];
    
    if (create.isThisYear) { // 今年
        if (create.isToday) { // 今天
            NSDateComponents *cmps = [[NSDate date] deltaFrom:create];
            
            if (cmps.hour >= 1) { // 时间差距 >= 1小时
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1小时 > 时间差距 >= 1分钟
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else { // 1分钟 > 时间差距
                return @"刚刚";
            }
        } else if (create.isYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            return [fmt stringFromDate:create];
        } else { // 其他
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:create];
        }
    } else { // 非今年
        return _created_at;
    }

}
-(CGFloat)cellHeight{
    if (!_cellHeight) {
     //   YKLog(@"\nb--\n%@\n%@\n%@--e\n",self.small_image,self.middle_image,self.large_image);
        CGSize maxSize = CGSizeMake(SCWidth-2*YKTopictCellMargin, MAXFLOAT);
        CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
        CGFloat cellH = YKTopictTextY + textH + YKTopictCellBottomBarH ;
        _cellHeight = cellH ;
        
        if (self.type == YKTopicTypePicture) {
            CGFloat imageW = maxSize.width;
            CGFloat imageH = imageW*self.height/self.width;
            CGFloat imageX = YKTopictCellMargin ;
            if (imageH>YKTopictCellPictureMaxH) {
                imageH = YKTopictCellPictureBreakH ;
                self.bigPicture = YES ;
            }
            CGFloat imageY = YKTopictTextY + textH ;
            _pictureVF = CGRectMake(imageX, imageY, imageW, imageH);
            _cellHeight += imageH ;
        }
        else if (self.type == YKTopicTypeVoice){
            CGFloat voiceX = YKTopictCellMargin ;
            CGFloat voiceY = YKTopictTextY + textH ;
            CGFloat voiceW = maxSize.width ;
            CGFloat voiceH = voiceW * self.height / self.width ;
            _voiceVF = CGRectMake(voiceX, voiceY, voiceW, voiceH);
            _cellHeight += voiceH ;
        }else if (self.type == YKTopicTypeVideo){
            CGFloat videoX = YKTopictCellMargin ;
            CGFloat videoY = YKTopictTextY + textH ;
            CGFloat videoW = maxSize.width ;
            CGFloat videoH = videoW * self.height / self.width ;
            _videoVF = CGRectMake(videoX, videoY, videoW, videoH);
            _cellHeight += videoH ;
        }
        YKComment *cmt = [self.top_cmt firstObject];
        if (cmt) {
            NSString *comment = [NSString stringWithFormat:@"%@ : %@",cmt.user.username,cmt.content];
            CGFloat cmtH = [comment boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height ;
            cmtH =  cmtH +  2 * YKTopictCellMargin ;
            _cellHeight += cmtH ;
        }
    }
    return  _cellHeight;

}
@end
